package com.unisv.dj;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.unisv.dj.R.id;
import com.unisv.dj.util.Command;

/**
 * <p>
 * init activity
 * </p>
 * 
 * @author fanzhen
 * @time 8:04:13 PM Sep 23, 2011
 * @email fanzhen.meng@gmail.com
 */
public class CoachActivity extends Activity {

	private static final String TAG = "AutoActivity";
	BroadcastReceiver commandReceiver;
	IntentFilter movementFilter;

	private ImageView imgLeft;
	private ImageView imgStart;
	private ImageView imgStop;
	private ImageView imgRight;
	private ImageView imgBack;
	private ImageView imgPause;
	private ImageView imgman;
	private TextView tvcountdown;
	private TextView tvsuspend;
	private TextView tvruntime;

	private CountDownTimer countDownTimer;
	private CountDownTimer suspendTimer;
	private int currentModel = 1;
	private boolean timerFlag = false;
	private int countDown = 35;
	private boolean isAutoCycle = false;
	private boolean isCountDownTimerRun = false;
	private boolean isSuspendTimerRun = false;

	void initStart() {
		suspendCancel();
		countDownCancel();
		tvruntime.setText(String.valueOf(countDown));
		tvcountdown.setText(String.valueOf(countDown));
		imgman.setImageDrawable(getResources()
				.getDrawable(R.drawable.coachman1));
	}

	void suspendCancel() {
		suspendTimer.cancel();
		tvsuspend.setText("0");
		isSuspendTimerRun = false;
	}

	void suspendStart() {
		suspendTimer.start();
		tvsuspend.setText("15");
		isSuspendTimerRun = true;
	}

	void countDownCancel() {
		countDownTimer.cancel();
		tvcountdown.setText("0");
		timerFlag = false;
		isCountDownTimerRun = false;
	}

	void countDownStart() {
		countDownTimer.start();
		tvcountdown.setText(String.valueOf(countDown));
		timerFlag = true;
		isCountDownTimerRun = true;
	}

	// public void doPause() {
	// // Pause
	// timerFlag = false;
	// countDownTimer.cancel();
	// isCountDownTimerRun = false;
	// suspendTimer.start();
	// isSuspendTimerRun = true;
	// Command.stopShort();
	// }

	public void changeModel() {
		if (currentModel == 1) {
			imgman.setImageDrawable(getResources().getDrawable(
					R.drawable.coachman2));
			currentModel = 2;
		} else if (currentModel == 2) {
			imgman.setImageDrawable(getResources().getDrawable(
					R.drawable.coachman3));
			currentModel = 3;
		} else if (currentModel == 3) {
			imgman.setImageDrawable(getResources().getDrawable(
					R.drawable.coachman4));
			currentModel = 4;
		} else if (currentModel == 4) {
			imgman.setImageDrawable(getResources().getDrawable(
					R.drawable.coachman5));
			currentModel = 5;
		} else {
			imgman.setImageDrawable(getResources().getDrawable(
					R.drawable.coachman1));
			currentModel = 1;
		}
	}

	public void commandSend(int model) {
		// 根据动作发命令
		// if (!isAutoCycle)
		// Command.start();
		if (currentModel == 1) {
			Command.changeSpeed(50, 50);
		} else if (currentModel == 2) {
			Command.changeSpeed(15, 15);
		} else if (currentModel == 3) {
			Command.changeSpeed(35, 35);
		} else if (currentModel == 4) {
			Command.changeSpeed(50, 50);
		} else if (currentModel == 5) {
			Command.changeSpeed(50, 50);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.coach);
		imgLeft = (ImageView) this.findViewById(id.imgLeft);
		imgStart = (ImageView) this.findViewById(id.imgStart);
		imgStop = (ImageView) this.findViewById(id.imgStop);
		imgRight = (ImageView) this.findViewById(id.imgRight);
		imgBack = (ImageView) this.findViewById(id.imgBack);
		imgPause = (ImageView) this.findViewById(id.imgPause);
		tvcountdown = (TextView) this.findViewById(id.tvcountdown);
		tvsuspend = (TextView) this.findViewById(id.tvsuspend);
		tvruntime = (TextView) this.findViewById(id.tvruntime);
		imgman = (ImageView) this.findViewById(id.imgman);

		imgLeft.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Left
				if (!timerFlag && !isSuspendTimerRun && !isCountDownTimerRun) {
					changeModel();
					// doPause();
				}
			}
		});

		imgStart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Start
				if (!timerFlag) {
					countDownStart();
					if (isSuspendTimerRun)
						suspendCancel();
					if (isAutoCycle) {
						changeModel();
						isAutoCycle = false;
					}
					commandSend(currentModel);
				}
			}
		});

		imgStop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Stop
				initStart();
				// Command.stopShort();
				Command.changeSpeed(0, 0);
			}
		});

		imgStop.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View arg0) {
				// Command.stopLong();
				Command.changeSpeed(0, 0);
				initStart();
				finish();
				return false;
			}
		});

		imgRight.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Right
				if (!timerFlag && !isSuspendTimerRun && !isCountDownTimerRun) {
					changeModel();
					// doPause();
				}
			}
		});

		imgBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Back
				Intent intent = new Intent(
						MediaStore.INTENT_ACTION_MUSIC_PLAYER);
				startActivity(intent);
			}
		});

		imgPause.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Pause
				Intent intent = new Intent();  
				intent.setAction(android.content.Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.EMPTY, "video/*");
				startActivity(intent);
			}
		});

		tvruntime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// runtime
				if (!timerFlag && !isCountDownTimerRun && !isSuspendTimerRun) {
					if (countDown < 60)
						countDown++;
					else
						countDown = 35;
					Log.d(TAG, String.valueOf(countDown));
					tvruntime.setText(String.valueOf(countDown));
				}
			}
		});

		suspendTimer = new CountDownTimer(15 * 1000L, 1000L) {
			@Override
			public void onTick(long millisUntilFinished) {
				int currentMinute = Integer.parseInt(tvsuspend.getText()
						.toString());
				currentMinute--;
				tvsuspend.setText(String.format("%1$d", currentMinute));
			}

			@Override
			public void onFinish() {
				tvsuspend.setText("0");
				isSuspendTimerRun = false;
				isAutoCycle = true;
				imgStart.performClick();
			}
		};

		countDownTimer = new CountDownTimer(countDown * 1000L, 1000L) {
			@Override
			public void onTick(long millisUntilFinished) {
				int currentMinute = Integer.parseInt(tvcountdown.getText()
						.toString());
				currentMinute--;
				tvcountdown.setText(String.format("%1$d", currentMinute));
			}

			@Override
			public void onFinish() {
				tvcountdown.setText("0");
				timerFlag = false;
				isCountDownTimerRun = false;
				Command.changeSpeed(0, 0);
				suspendStart();
			}
		};

		initStart();
	}

	@Override
	public void onResume() {
		if (movementFilter == null)
			movementFilter = new IntentFilter(ServerService.COMMAND_COMING);
		if (commandReceiver == null)
			commandReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					// Log.i(TAG, "msg from service.............." + command);
					byte[] comm = intent
							.getByteArrayExtra(ServerService.COMMAND_TAG);
					if (comm.length == 4 && comm[0] == 111) {
						switch (comm[3]) {
						case 1:
							imgStart.performClick();
							break;
						case 2:
							imgStop.performClick();
							break;
						case 3:
							imgStop.performLongClick();
							break;
						case 4:
							imgLeft.performClick();
							break;
						case 5:
							imgRight.performClick();
							break;
						case 6:
							imgLeft.performClick();
							break;
						case 7:
							imgRight.performClick();
							break;

						default:
							break;
						}
					}
				}
			};
		registerReceiver(commandReceiver, movementFilter);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (commandReceiver != null) {
			unregisterReceiver(commandReceiver);
			commandReceiver = null;
		}
		if (movementFilter != null)
			movementFilter = null;
		super.onDestroy();
	}
}
